/*
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved.
 */
package com.hbisdt.cet.sample.controller;

import com.hbisdt.cet.sample.entity.dto.CarbonArticleRequest;
import com.hbisdt.cet.sample.entity.po.CarbonArticle;
import com.hbisdt.cet.sample.service.ICarbonArticleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 路由控制器
 *
 * @author : 教学示例
 * @version : 1.0
 * @date : 2024-12-19 10:27
 */
@Controller
public class RouteController {

    @Resource
    private ICarbonArticleService carbonArticleService;
    /**
     * 路由页面-登录页
     *
     * @return java.lang.String
     * @author : 教学示例
     */
    @RequestMapping("/view/login")
    public String login() {
        return "login";
    }

    /**
     * 路由页面-列表
     *
     * @return java.lang.String
     * @author : 教学示例
     */
    @RequestMapping("/view/index")
    public String articleList(Model model) {
        List<CarbonArticle> articleList = carbonArticleService.getCarbonArticleList();
        model.addAttribute("articleList", articleList);
        return "articleList";
    }

    /**
     * 路由页面-新增
     *
     * @return java.lang.String
     * @author : 教学示例
     */
    @RequestMapping("/view/insert")
    public String insertPage() {
        return "insertPage";
    }

    /**
     * 路由页面-编辑
     *
     * @return java.lang.String
     * @author : 教学示例
     */
    @RequestMapping("/view/edit/{articleId}")
    public String editPage(@PathVariable Long articleId, Model model) {
        CarbonArticle article = carbonArticleService.getById(articleId);
        model.addAttribute("article", article);
        return "editPage";
    }

    /**
     * 新增资讯
     *
     * @param carbonArticleRequest 请求实体
     * @return java.lang.String
     * @author : 教学示例
     */
    @RequestMapping("/carbon/insert")
    public String insertData(CarbonArticleRequest carbonArticleRequest) {
        carbonArticleService.addCarbonArticle(carbonArticleRequest);
        return "redirect:/view/index";
    }

    /**
     * 编辑资讯
     * @param carbonArticleRequest
     * @return java.lang.String
     * @author : 教学示例
     */
    @RequestMapping("/carbon/edit")
    public String editData(CarbonArticleRequest carbonArticleRequest) {
        carbonArticleService.editCarbonArticle(carbonArticleRequest);
        return "redirect:/view/index";
    }

    /**
     * 删除资讯
     *
     * @param articleId 资讯id
     * @return java.lang.String
     * @author : 教学示例
     */
    @RequestMapping("/carbon/delete/{articleId}")
    public String deleteData(@PathVariable Long articleId) {
        CarbonArticleRequest request = new CarbonArticleRequest();
        request.setArticleId(articleId);
        carbonArticleService.deleteCarbonArticle(request);
        return "redirect:/view/index";
    }
}
